package newapproach.elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import newapproach.writer.CoutXmlElement;
import newapproach.writer.XmlElement;

import org.dom4j.Element;

public class YTable extends YElement{

	private List<YColumn> yColumns;

	public YTable(Element table) {
		super(table);
		yColumns = new ArrayList<YColumn>(); 
		for(Iterator<Element> it =table.elementIterator(); it.hasNext();){
			Element column = it.next();
			YColumn yColumn = new YColumn(column);
			yColumns.add(yColumn);
		}
	}

	@Override
	public XmlElement toJsp() {
		List<XmlElement> xmlColumns = new ArrayList<XmlElement>();
		for(YColumn yColumn : yColumns){
			xmlColumns.add(new XmlElement("td", null, new CoutXmlElement("row."+yColumn.getName())));
		}
		Map<String,String> forEachAtt = new HashMap<String,String>();
		forEachAtt.put("items", j_var(getName()));
		forEachAtt.put("var", "row");
		XmlElement forEachXmlElement = new XmlElement("c:forEach", forEachAtt, xmlColumns);
		XmlElement tableJspElement = new XmlElement("table", null, forEachXmlElement);
		return tableJspElement;
	}

	@Override
	public String toBean() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String toStringContent() {
		String str = "";
		for(YColumn col : yColumns){
			str += col.toString();
		}
		return str;
	}


}
